package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.JoinIn;
import com.antdesignbackend.backend.service.JoinInService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 申请收藏
 */
@Controller
@RequestMapping("/joinIn")
public class JoinInController {
    private Integer menuId;

    @Autowired
    JoinInService joinInService;

    @GetMapping("/index")
    @AuthToken
    public String index(Integer menuId){
        this.menuId = menuId;
        return "joinIn/index";
    }

    /**
     * 分页查询
     * @param joinIn
     * @param request
     * @return
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    @AuthToken
    public ReturnVO tableSearch(@RequestBody JoinIn joinIn, HttpServletRequest request) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        ReturnVO returnVO = new ReturnVO();
        List<JoinIn> datalist=new ArrayList<>();
        QueryWrapper<JoinIn> wrapper = new QueryWrapper<JoinIn>();
        if (joinIn.getName()!=null && !joinIn.getName().equals("")){
            wrapper.like("name",joinIn.getName());
        }
        if (joinIn.getPhone()!=null && !joinIn.getPhone().equals("")){
            wrapper.like("phone",joinIn.getPhone());
        }
        if(joinIn.getIsShop() != null && joinIn.getIsShop() != -1){
            wrapper.eq("isShop",joinIn.getIsShop());
        }
        if (joinIn.getTypes()!=null){
            if (joinIn.getTypes() != -1){
                wrapper.eq("types",joinIn.getTypes());
            }
        } else  {
            wrapper.eq("types",0);
        }
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total = joinInService.count(wrapper);
        returnVO.setTotal(total);
        //再查询
        if(total>0)
        {
            datalist= joinInService.getBaseMapper().selectPage(new Page<>(joinIn.getPageIndex(),joinIn.getPageSize()),wrapper).getRecords();
            //得到当前菜单具备的功能权限
            //List<Button> list = permissionService.filterPermission(String.valueOf(roleID), menuId);
            //datalist.forEach(model -> model.setButtonList(list));
        }
        returnVO.setData(datalist);
        return returnVO;
    }

    /***
     * 根据id删除
     */
    @DeleteMapping("/deleteById/{id}")
    @ResponseBody
    public ReturnVO deleteById(@PathVariable Serializable id) {
        ReturnVO result = new ReturnVO();
        if (joinInService.removeById(Integer.parseInt(id.toString()))) {
            result.setMessage("删除成功");
            result.setCode("200");
        } else {
            result.setMessage("删除失败");
            result.setCode("500");
        }
        return result;
    }

    /***
     * 处理
     */
    @PostMapping("/handle")
    @ResponseBody
    public ReturnVO handle(@RequestBody JoinIn joinIn) {
        ReturnVO result = new ReturnVO();
        joinIn.setTypes(1);
        joinIn.setHandleTime(new Date());
        if (joinInService.updateById(joinIn)) {
            result.setMessage("处理成功");
            result.setCode("200");
        } else {
            result.setMessage("处理失败");
            result.setCode("500");
        }
        return result;
    }
}
